package com.baizhi.controller;

import com.baizhi.dto.Phone;
import com.baizhi.entity.user;
import com.baizhi.service.UserService;
import com.baizhi.util.SendSmsUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Resource
    UserService userService;
    @RequestMapping("queryAllPage")
    public HashMap<String, Object> queryAllPage(Integer page,Integer pageSize){
        log.info("当前页:{}",page);
        log.info("每页展示得条数:{}",pageSize);
        HashMap<String, Object> map = userService.queryAllPage(page, pageSize);

        return map;
    }
    @RequestMapping("delete")
    public HashMap<String,Object> delete(@RequestBody user user){
        log.info("删除的数据：{}",user);
        HashMap<String, Object> map = userService.delete(user);
        return map;
    }
    @RequestMapping("update")
    public HashMap<String,Object> update(@RequestBody user user){
        log.info("修改的数据:{}",user);

        return userService.update(user);

    }
    @RequestMapping("add")
    public HashMap<String,Object> add(@RequestBody user user){
        log.info("添加的数据:{}",user);
      return userService.add(user);
    }
    @RequestMapping("uploadHeadImg")
    public HashMap<String, String> uploadHeadImg(MultipartFile headImg){
        log.info("文件名:{}",headImg.getOriginalFilename());
        log.info("文件大小:{}",headImg.getSize());
        log.info("文件类型:{}",headImg.getContentType());

        String msg = userService.uploadHeadImgALiY(headImg);
        HashMap<String, String> map = new HashMap<>();

        map.put("fileName",msg);
        return map;
    }
    @RequestMapping("queryById")
    public user queryById(String id){
       return userService.queryById(id);
    }
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping("getPhoneCode")
    public HashMap<String,Object> getPhoneCode(String phone){
        System.out.println("手机号："+phone);

        //1.获取随机数
        String random = SendSmsUtil.getRandom(6);

        System.out.println("手机验证码："+phone);

        //2.存储验证码  设置过期时间
        //设置一个key同时 指定key超时时间
        stringRedisTemplate.opsForValue().set(phone,random,60, TimeUnit.SECONDS);

        //3.发送验证码
        String msg = SendSmsUtil.sendPhone(phone, random);

        HashMap<String, Object> map = new HashMap<>();

        map.put("message",msg);

        return map;
    }

    /**验证手机验证码*/
    @RequestMapping("phoneCheck")
    public HashMap<String,Object> phoneCheck(@RequestBody Phone phone){

        System.out.println("验证对象："+phone);

        HashMap<String, Object> map = new HashMap<>();

        //1.判断key是否存在
        Boolean aBoolean = stringRedisTemplate.hasKey(phone.getPhoneNum());

        if(aBoolean){
            //存在  没有过期
            String phoneCode = stringRedisTemplate.opsForValue().get(phone.getPhoneNum());

            //取出验证码做判断
            if(phoneCode.equals(phone.getCodes())){
                //一致  验证通过
                map.put("message","验证码通过");
            }else{
                //不一致 验证码不正确
                map.put("message","验证码不正确");
            }
        }else{
            //不存在  验证码已经过期  返回提示信息
            map.put("message","验证码不正确");
        }
        return map;
    }

}
